package com.oot.twitter.info;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class User {
	protected String user_id, username, full_name, avatar, status;
    protected String order_id; // optional property

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

    public String getOrder_id() {
        return order_id;
    }

    public void setOrder_id(String order_id) {
        this.order_id = order_id;
    }

    public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getFull_name() {
		return full_name;
	}

	public void setFull_name(String full_name) {
		this.full_name = full_name;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public Bitmap getAvatarBitmap() {
		Bitmap bitmap = null;
    	try {
    		bitmap = BitmapFactory.decodeStream((InputStream)new URL(getAvatar()).getContent());
		} catch (MalformedURLException e) {
			System.out.println("Error in decode avatar bitmap");
		} catch (IOException e) {
			System.out.println("Error in io avatar bitmap");
		}
    	return bitmap;
	}
	
	public User(){
		
	}
	
	public User(JSONObject jsonObject) {
		TJSonParser parser = new TJSonParser(jsonObject);
		user_id   = parser.getString("user_id","");
		username  = parser.getString("username","");
		full_name = parser.getString("full_name","");
		avatar    = parser.getString("avatar","");
		status    = parser.getString("status","");
		order_id  = parser.getString("order_id","0");
	}

    public static ArrayList<User> parseArray(JSONArray jsonArray){
        ArrayList<User> users = new ArrayList<User>();
        try{
            for (int i=0 ; i< jsonArray.length(); i++)
                users.add(new User((JSONObject)jsonArray.get(i)));
        }catch (Exception e){
            System.out.println("Error in parse json array");
        }
        return users;
    }
}
